package me.notelife.svnmonitor.auto;

import java.util.Collection;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class RepositoryMonitor implements Runnable
{
    private MyRepository resp;

    private SVNRepository repository = null;

    public void initialize()
    {
        DAVRepositoryFactory.setup();
        try
        {
            repository = SVNRepositoryFactory.create( SVNURL.parseURIEncoded( resp.getUrl() ) );
            ISVNAuthenticationManager authManager =
                SVNWCUtil.createDefaultAuthenticationManager( resp.getUsername(), resp.getPassword() );
            repository.setAuthenticationManager( authManager );
        }
        catch( SVNException svne )
        {
            System.err.println( "error while creating an SVNRepository for the location '" + resp.getUrl() + "': " +
                svne.getMessage() );
        }
    }

    @Override
    public void run()
    {
        long endRevision = resp.getEndRevision();
        try
        {
            if( endRevision < 0 )
            {
                endRevision = repository.getLatestRevision();
            }

            Collection logEntries = null;
            logEntries = repository.log( new String[]{ "" }, null, resp.getStartRevision(), endRevision, true, true );

        }
        catch( SVNException svne )
        {
            System.err.println( "error while operating: " + svne.getMessage() );
        }
    }

    public void setResp( MyRepository resp )
    {
        this.resp = resp;
    }
}
